package org.example.project

import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.compose.LocalLifecycleOwner
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
import kotlin.time.Duration
import kotlin.time.Duration.Companion.seconds

@Composable
internal fun PicCarouselPage(modifier: Modifier = Modifier) {

    println("log--> 123123")

    Column(
        modifier = modifier.fillMaxSize()
            .background(Color.Black)
    ) {
        Test()

        Column(
            modifier = Modifier.fillMaxSize(),
            horizontalAlignment = Alignment.CenterHorizontally,
            verticalArrangement = Arrangement.Center
        ) {
            Carousel2()
        }
    }

}

@Composable
fun Test(
    modifier: Modifier = Modifier,
    lifecycleOwner: LifecycleOwner = LocalLifecycleOwner.current,
) {

    var timer = CoroutineTimer()
    timer.start { println("log--> Hello World") }
    Box(
        modifier = Modifier.fillMaxWidth()
            .padding(20.dp)

    ) {
        println("log--> Hello World")
        Text(
            "Hello World",
            fontSize = 18.sp,
            color = Color(0x4cff5000),
            modifier = Modifier
                .border(0.5.dp, Color(0xffff5000))
                .clickable {
                    timer.stop()
                }
                .padding(start = 6.dp, end = 6.dp, top = 2.dp, bottom = 2.dp)
        )
    }
}

class CoroutineTimer {
    private var job: Job? = null

    // 启动定时器
    fun start(interval: Duration = 1.seconds, block: suspend () -> Unit) {
        println("log--> job start")
        job = GlobalScope.launch {
            while (isActive) {
                block()
                delay(interval)
            }
        }
    }

    // 停止定时器
    fun stop() {
        println("log--> job stop")
        job?.cancel()
        job = null
    }
}